#include "db.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"
#define HOST "127.0.0.1" 
#define USER "root"
#define PWD ""
#define DBNAME "gobang" 
#define PORT 3306


void mysql_test()
{
    MYSQL* mysql = UtilMysql::mysql_create(HOST,USER,PWD,DBNAME,PORT);
    std::string sql = "insert student values(null,'小黑',22,55.5,66.5,77.5);"; 
    bool ret = UtilMysql::mysql_exec(mysql,sql); 
    if(!ret)
    {
        return;
    }
    UtilMysql::mysql_destroy(mysql);
}

void json_test()
{
    Json::Value root;
    root["姓名"]  = "张三"; 
    root["年龄"] = 18; 
    root["成绩"].append(88.5);
    root["成绩"].append(98.5);
    root["成绩"].append(93.5);
    std::string body ; 

    UtilJson::serialize(root,body);
    DLOG("%s",body.c_str());

    Json::Value val; 
    UtilJson::unserialize(body,val);
    std::cout << "姓名 : " << val["姓名"].asString() << std::endl; 
    std::cout << "年龄 : " << val["年龄"].asInt() << std::endl; 
    int sz = val["成绩"].size();
    for(int i = 0 ;  i < sz ; i ++)
    {
        std::cout << "成绩"<< i + 1 << " : " << val["成绩"][i] << std::endl; 
    }
}

void string_test()
{
    std::string src = ",,,123,456,,,,789,,,,,,";
    std::vector<std::string> arry;
    UtilString::split(src,",",arry);
    for(auto& s : arry)
    {
        DLOG("%s", s.c_str());
    }
}

void file_test()
{
  std::string filename = "./Makefile"; 
  std::string body; 
  size_t size = 0 ; 
  UtilFile::read(filename,body,size);
  std::cout << body << std::endl ;
}

void db_test()
{
    user_table ut(HOST,USER,PWD,DBNAME,PORT);  
    Json::Value root;
    // root["username"] = "xiaoming";
    // root["password"] = "12345678";
    // ut.insert(root);

  //  ut.login(root);
    ut.lose(1);
    Json::Value val;
    ut.select_by_name("xiaoming",val);
    std::string body;

    std::cout << body << std::endl;
    std::cout << "用户名 : " << val["username"].asString() << std::endl; 
    std::cout << "ID : " << val["id"].asInt() << std::endl; 
    std::cout << "分数 : " << val["score"].asInt() << std::endl; 
    std::cout << "总场数 : " << val["total_count"].asInt() << std::endl; 
    std::cout << "胜场数 : " << val["win_count"].asString() << std::endl; 
    
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr con;
    om.enter_game_hall(1,con);
    if(om.is_in_game_hall(1)) 
    {
      DLOG("IN GAME HALL!");
    }else 
    {
      DLOG("NOT IN GAME HALL!");
    }
    om.exit_game_hall(1); 
    if(om.is_in_game_hall(1)) 
    {
      DLOG("IN GAME HALL!");
    }else 
    {
      DLOG("NOT IN GAME HALL!");
    }
}
int main()
{
    gobang_server _server(HOST,USER,PWD,DBNAME);
    _server.start(8081);
    return 0;
}
